package itxx.itxx;

import itxx.itxx.bean.Studnet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/ServletDemo4")
public class ServletDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* 获取请求参数并封装对象
* */
    //手动封装
        /*
        1.获取所有数据
        2.封装对象
        3.输出对象
       */


      /*
      通过反射封装对象
        1.  获取所有的数据
        2.  封装对象
            2.1     遍历集合
            2.2      获取属性描述器
            2.3获取setXXX方法
            2.4执行方法

//        1.  获取所有的数据
        Map<String, String[]> map = request.getParameterMap();
        //封装学生对象
        Studnet stu = new Studnet();
        //遍历集合
        for (String name:map.keySet()){//name  --> username  password
            String[] value = map.get(name);//zhangsan
            try {
                //2.2获取Student对象的属性描述器
                PropertyDescriptor pd = new PropertyDescriptor(name, stu.getClass());
                //2.3获取对应的setXXX方法
                // 2.4执行方法
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }

        }
 */

        /*
        工具类封装
            beanutils
        * */
        Map<String, String[]> map = request.getParameterMap();
        Studnet stu = new Studnet();
        try {
            BeanUtils.populate(stu,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(stu);
    }
}
